package com.wildwolf.project.wildwolf.adapter;

import java.util.List;

import com.wildwolf.project.wildwolf.R;
import com.wildwolf.project.wildwolf.config.AppConfig;
import com.wildwolf.project.wildwolf.model.CustomizationContact;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class AddressBookExpandableListAdapter extends BaseExpandableListAdapter {
/**
 * 分组数据
 */
private List<String> addressBookGroup;   
/**
 *分组 节点数据
 */
private List<List<CustomizationContact>>  addressBookChild;
 /**
  * 上下文
  */
private  Context  context;
	public AddressBookExpandableListAdapter(List<String> addressBookGroup,
			List<List<CustomizationContact>> addressBookChild, Context context) {
		super();
		this.addressBookGroup = addressBookGroup;
		this.addressBookChild = addressBookChild;
		this.context = context;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return addressBookChild.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		String group = addressBookGroup.get(groupPosition);
		LayoutInflater inflater = LayoutInflater.from(context);
		final CustomizationContact customizationContact = addressBookChild.get(groupPosition).get(childPosition);
		View childView  = null;
		if(group.contains(AppConfig.AdressBookKey.ENCOUNTER_GROUP_PREFIX)){
			childView =	inflater.inflate(R.layout.addressbook_expandable_child, null);
			TextView nike = (TextView)childView.findViewById(R.id.addressbook_textView_nike); 
			nike.setText(customizationContact.getName());
			TextView autograph = (TextView)childView.findViewById(R.id.addressbook_textView_autograph);
			autograph.setText(customizationContact.getAutograph());
		}else
		{
			childView =	inflater.inflate(R.layout.addressbook_expandable_child_phone, null);
			TextView nike = (TextView)childView.findViewById(R.id.addressbook_textView_nike); 
			nike.setText(customizationContact.getName());
			TextView autograph = (TextView)childView.findViewById(R.id.addressbook_textView_autograph);
			autograph.setText(customizationContact.getAutograph());
			TextView invite = (TextView)childView.findViewById(R.id.addressbook_textView_invite);
			invite.setOnClickListener(new  View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Toast.makeText(context, context.getString(R.string.addressbook_invite_toast, customizationContact.getName()), Toast.LENGTH_SHORT).show();
				}
			});
		}
	
		
		return childView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if(addressBookChild.size()==0){
			return 0;
		}
		return addressBookChild.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return addressBookGroup.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return addressBookGroup.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		String group = addressBookGroup.get(groupPosition);
		LayoutInflater inflater = LayoutInflater.from(context);
		View groupView =	inflater.inflate(R.layout.addressbook_expandable_group, null);
		TextView groupTetView =	(TextView) groupView.findViewById(R.id.addressbook_expandable_textView_group);
		groupTetView.setText(group);
		return groupView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
